﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PlateNumber {
    public partial class OptionForm : Form {
        public OptionForm() {
            InitializeComponent();
        }

        private Option _option = null;

        public Option Option { get; set; }

        private void OptionForm_Load(object sender, EventArgs e) {
            lblVersion.Text = "版本: " + Application.ProductVersion;

            if (Option != null) {
                _option = Option;
            }
            else {
                _option = Option.Load();
            }

            //
            // 常规
            //

            txtGeneralDomain.Text = _option.General.Domain;
            txtGeneralHome.Text = _option.General.Home;

            //
            // 网络连接
            //

            rbtnNetworkLocal.Checked = _option.Network.ConnectionType == NetworkConnectionType.Local;
            rbtnNetworkProxy.Checked = _option.Network.ConnectionType == NetworkConnectionType.Proxy;
            rbtnNetworkPpp.Checked = _option.Network.ConnectionType == NetworkConnectionType.Ppp;
            //chkNetworkUsingStaticProxyAtFirst.Checked = _option.Network.UsingStaticProxyAtFirst;
            numNetworkAvailableProxyServerCountLowLimit.Value = _option.Network.AvailableProxyServerCountLowLimit;
            numNetworkStaticProxyServerResponseTime.Value = _option.Network.StaticProxyServerResponseTime;
            numNetworkInvalidProxyServerResponseTime.Value = _option.Network.InvalidProxyServerResponseTime;

            //
            // 任务
            //

            numTaskStep0Delay.Value = _option.Task.Step0Delay;
            numTaskStep1Delay.Value = _option.Task.Step1Delay;
            numTaskStep2Delay.Value = _option.Task.Step2Delay;
            numTaskStep3Delay.Value = _option.Task.Step3Delay;
            numTaskStep4Delay.Value = _option.Task.Step4Delay;
            numTaskIPSwitchPeriod.Value = _option.Task.IPSwitchPeriod;
            ddlTaskIfFoundUnknownPage.SelectedIndex = (int)_option.Task.IfFoundUnknownPage;
            ddlTaskIfNumberNotExist.SelectedIndex = (int)_option.Task.IfNumberNotExist;
            ddlTaskIfNumberHasBeUsed.SelectedIndex = (int)_option.Task.IfNumberHasBeUsed;
            chkTaskCirculateTaskList.Checked = _option.Task.CirculateTaskList;

            chkTaskIfFoundUnknownPageRetryTimes.Checked = _option.Task.IfFoundUnknownPageRetryTimes > 0;
            numTaskIfFoundUnknownPageRetryTimes.Value = Math.Max(1, _option.Task.IfFoundUnknownPageRetryTimes);
            chkTaskIfFoundUnknownPageRetryTimes.Visible = _option.Task.IfFoundUnknownPage == PlateNumber.Option.IfFoundUnknownPage.Retry;
            numTaskIfFoundUnknownPageRetryTimes.Visible = _option.Task.IfFoundUnknownPage == PlateNumber.Option.IfFoundUnknownPage.Retry;
            lblTaskIfFoundUnknownPageRetryTimes.Visible = _option.Task.IfFoundUnknownPage == PlateNumber.Option.IfFoundUnknownPage.Retry;
            numTaskIfFoundUnknownPageRetryTimes.Enabled = chkTaskIfFoundUnknownPageRetryTimes.Checked;
            lblTaskIfFoundUnknownPageRetryTimes.Enabled = chkTaskIfFoundUnknownPageRetryTimes.Checked;

            chkTaskIfNumberNotExistRetryTimes.Checked = _option.Task.IfNumberNotExistRetryTimes > 0;
            numTaskIfNumberNotExistRetryTimes.Value = Math.Max(1, _option.Task.IfNumberNotExistRetryTimes);
            chkTaskIfNumberNotExistRetryTimes.Visible = _option.Task.IfNumberNotExist == PlateNumber.Option.IfNumberNotExist.Retry;
            numTaskIfNumberNotExistRetryTimes.Visible = _option.Task.IfNumberNotExist == PlateNumber.Option.IfNumberNotExist.Retry;
            lblTaskIfNumberNotExistRetryTimes.Visible = _option.Task.IfNumberNotExist == PlateNumber.Option.IfNumberNotExist.Retry;
            numTaskIfNumberNotExistRetryTimes.Enabled = chkTaskIfNumberNotExistRetryTimes.Checked;
            lblTaskIfNumberNotExistRetryTimes.Enabled = chkTaskIfNumberNotExistRetryTimes.Checked;

            chkTaskIfNumberHasBeUsedRetryTimes.Checked = _option.Task.IfNumberHasBeUsedRetryTimes > 0;
            numTaskIfNumberHasBeUsedRetryTimes.Value = Math.Max(1, _option.Task.IfNumberHasBeUsedRetryTimes);
            chkTaskIfNumberHasBeUsedRetryTimes.Visible = _option.Task.IfNumberHasBeUsed == PlateNumber.Option.IfNumberHasBeUsed.Retry;
            numTaskIfNumberHasBeUsedRetryTimes.Visible = _option.Task.IfNumberHasBeUsed == PlateNumber.Option.IfNumberHasBeUsed.Retry;
            lblTaskIfNumberHasBeUsedRetryTimes.Visible = _option.Task.IfNumberHasBeUsed == PlateNumber.Option.IfNumberHasBeUsed.Retry;
            numTaskIfNumberHasBeUsedRetryTimes.Enabled = chkTaskIfNumberHasBeUsedRetryTimes.Checked;
            lblTaskIfNumberHasBeUsedRetryTimes.Enabled = chkTaskIfNumberHasBeUsedRetryTimes.Checked;


            // 代理服务器列表
            dgvNetworkProxyList.DataSource = ProxyServer.GetAvailableProxyServers();

            // 拨号连接列表
            dgvNetworkPppList.DataSource = PPP.Load();
            dgvNetworkPppList.ClearSelection();

            UpdateUI();
        }

        private void UpdateUI() {
            //chkNetworkUsingStaticProxyAtFirst.Enabled = rbtnNetworkProxy.Checked;
            //dgvNetworkProxyList.Enabled = rbtnNetworkProxy.Checked && chkNetworkUsingStaticProxyAtFirst.Checked;
            //btnNetworkProxyAdd.Enabled = rbtnNetworkProxy.Checked && chkNetworkUsingStaticProxyAtFirst.Checked;
            //btnNetworkProxyEdit.Enabled = rbtnNetworkProxy.Checked && chkNetworkUsingStaticProxyAtFirst.Checked;
            //btnNetworkProxyDelete.Enabled = rbtnNetworkProxy.Checked && chkNetworkUsingStaticProxyAtFirst.Checked;

            //dgvNetworkPppList.Enabled = rbtnNetworkPpp.Checked;
            //btnNetworkPppAdd.Enabled = rbtnNetworkPpp.Checked;
            //btnNetworkPppEdit.Enabled = rbtnNetworkPpp.Checked;
            //btnNetworkPppDelete.Enabled = rbtnNetworkPpp.Checked;
            //btnNetworkPppDefault.Enabled = rbtnNetworkPpp.Checked;
        }

        private void btnOK_Click(object sender, EventArgs e) {
            //
            // 常规
            //

            _option.General.Domain = txtGeneralDomain.Text.Trim();
            _option.General.Home = txtGeneralHome.Text.Trim();

            //
            // 网络连接
            //

            //_option.Network.UsingStaticProxyAtFirst = chkNetworkUsingStaticProxyAtFirst.Checked;
            _option.Network.AvailableProxyServerCountLowLimit = (int)numNetworkAvailableProxyServerCountLowLimit.Value;
            _option.Network.StaticProxyServerResponseTime = (int)numNetworkStaticProxyServerResponseTime.Value;
            _option.Network.InvalidProxyServerResponseTime = (int)numNetworkInvalidProxyServerResponseTime.Value;
            if (rbtnNetworkLocal.Checked) {
                _option.Network.ConnectionType = NetworkConnectionType.Local;
            }
            if (rbtnNetworkProxy.Checked) {
                _option.Network.ConnectionType = NetworkConnectionType.Proxy;
            }
            if (rbtnNetworkPpp.Checked) {
                _option.Network.ConnectionType = NetworkConnectionType.Ppp;
            }

            //
            // 任务
            //

            _option.Task.Step0Delay = (int)numTaskStep0Delay.Value;
            _option.Task.Step1Delay = (int)numTaskStep1Delay.Value;
            _option.Task.Step2Delay = (int)numTaskStep2Delay.Value;
            _option.Task.Step3Delay = (int)numTaskStep3Delay.Value;
            _option.Task.Step4Delay = (int)numTaskStep4Delay.Value;
            _option.Task.IPSwitchPeriod = (int)numTaskIPSwitchPeriod.Value;
            _option.Task.IfFoundUnknownPage = (PlateNumber.Option.IfFoundUnknownPage)ddlTaskIfFoundUnknownPage.SelectedIndex;
            _option.Task.IfNumberNotExist = (PlateNumber.Option.IfNumberNotExist)ddlTaskIfNumberNotExist.SelectedIndex;
            _option.Task.IfNumberHasBeUsed = (PlateNumber.Option.IfNumberHasBeUsed)ddlTaskIfNumberHasBeUsed.SelectedIndex;
            _option.Task.CirculateTaskList = chkTaskCirculateTaskList.Checked;
            _option.Task.IfFoundUnknownPageRetryTimes = chkTaskIfFoundUnknownPageRetryTimes.Checked ? (int)numTaskIfFoundUnknownPageRetryTimes.Value : 0;
            _option.Task.IfNumberNotExistRetryTimes = chkTaskIfNumberNotExistRetryTimes.Checked ? (int)numTaskIfNumberNotExistRetryTimes.Value : 0;
            _option.Task.IfNumberHasBeUsedRetryTimes = chkTaskIfNumberHasBeUsedRetryTimes.Checked ? (int)numTaskIfNumberHasBeUsedRetryTimes.Value : 0;
           


            //
            // 保存
            //

            Option.Save(_option);
            Option = _option;
        }

        private void rbtnNetworkLocal_CheckedChanged(object sender, EventArgs e) {
            UpdateUI();
        }

        private void rbtnNetworkProxy_CheckedChanged(object sender, EventArgs e) {
            UpdateUI();
        }

        private void rbtnNetworkPpp_CheckedChanged(object sender, EventArgs e) {
            UpdateUI();
        }

        //private void chkNetworkUsingStaticProxyAtFirst_CheckedChanged(object sender, EventArgs e) {
        //    _option.Network.UsingStaticProxyAtFirst = chkNetworkUsingStaticProxyAtFirst.Checked;
        //    UpdateUI();
        //}

        private void btnNetworkPppAdd_Click(object sender, EventArgs e) {
            PppPropertyForm frm = new PppPropertyForm();
            frm.PPP = new PPP() {
                Id = 0,
                IsDefault = false
            };
            if (frm.ShowDialog(this) == DialogResult.OK) {
                PPP.Save(frm.PPP);
                dgvNetworkPppList.DataSource = PPP.Load();
                dgvNetworkPppList.ClearSelection();
            }
        }

        private void btnNetworkPppEdit_Click(object sender, EventArgs e) {
            int id = Int32.Parse(dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppId"].Value.ToString());
            string entryName = dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppEntryName"].Value.ToString();
            
            PppPropertyForm frm = new PppPropertyForm();
            frm.Text = entryName + " 属性";
            frm.PPP = PPP.Get(id);

            if (frm.ShowDialog(this) == DialogResult.OK) {
                PPP.Save(frm.PPP);
                dgvNetworkPppList.DataSource = PPP.Load();
                dgvNetworkPppList.ClearSelection();
            }
        }

        private void btnNetworkPppDelete_Click(object sender, EventArgs e) {
            int id = Int32.Parse(dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppId"].Value.ToString());
            string entryName = dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppEntryName"].Value.ToString();

            if (MessageBox.Show(this, "确定要删除 " + entryName + " 吗？", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
                PPP.Delete(id);
                dgvNetworkPppList.DataSource = PPP.Load();
                dgvNetworkPppList.ClearSelection();
            }
        }

        private void btnNetworkPppSetDefault_Click(object sender, EventArgs e) {
            int id = Int32.Parse(dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppId"].Value.ToString());
            PPP.SetDefault(id);
            dgvNetworkPppList.DataSource = PPP.Load();
            dgvNetworkPppList.ClearSelection();
        }

        private void dgvNetworkPppList_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            btnNetworkPppEdit.PerformClick();
        }

        private void dgvNetworkPppList_SelectionChanged(object sender, EventArgs e) {
            btnNetworkPppEdit.Enabled = dgvNetworkPppList.SelectedRows.Count > 0;

            btnNetworkPppDelete.Enabled = dgvNetworkPppList.SelectedRows.Count > 0 &&
                !Boolean.Parse(dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppIsDefault"].Value.ToString());

            btnNetworkPppSetDefault.Enabled = dgvNetworkPppList.SelectedRows.Count > 0 &&
                !Boolean.Parse(dgvNetworkPppList.SelectedRows[0].Cells["dgvcolPppIsDefault"].Value.ToString());
        }

        private void dgvNetworkProxyList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
            if (dgvNetworkProxyList.Columns[e.ColumnIndex].DataPropertyName == "Sign") {
                switch ((ProxyServerSign)e.Value) {
                    case ProxyServerSign.Invalid:
                        e.Value = "无效";
                        break;
                    case ProxyServerSign.Normal:
                        e.Value = "正常";
                        break;
                    case ProxyServerSign.Static:
                        e.Value = "静态";
                        break;
                }
            }

            if (dgvNetworkProxyList.Columns[e.ColumnIndex].DataPropertyName == "Delay") {
                if (e.Value.ToString() == Int32.MaxValue.ToString()) {
                    e.Value = "未知";
                }
            }
        }

        private void ddlTaskIfFoundUnknownPage_SelectedIndexChanged(object sender, EventArgs e) {
            chkTaskIfFoundUnknownPageRetryTimes.Visible = (PlateNumber.Option.IfFoundUnknownPage)ddlTaskIfFoundUnknownPage.SelectedIndex == PlateNumber.Option.IfFoundUnknownPage.Retry;
            numTaskIfFoundUnknownPageRetryTimes.Visible = (PlateNumber.Option.IfFoundUnknownPage)ddlTaskIfFoundUnknownPage.SelectedIndex == PlateNumber.Option.IfFoundUnknownPage.Retry;
            lblTaskIfFoundUnknownPageRetryTimes.Visible = (PlateNumber.Option.IfFoundUnknownPage)ddlTaskIfFoundUnknownPage.SelectedIndex == PlateNumber.Option.IfFoundUnknownPage.Retry;
        }

        private void ddlTaskIfNumberNotExist_SelectedIndexChanged(object sender, EventArgs e) {
            chkTaskIfNumberNotExistRetryTimes.Visible = (PlateNumber.Option.IfNumberNotExist)ddlTaskIfNumberNotExist.SelectedIndex == PlateNumber.Option.IfNumberNotExist.Retry;
            numTaskIfNumberNotExistRetryTimes.Visible = (PlateNumber.Option.IfNumberNotExist)ddlTaskIfNumberNotExist.SelectedIndex == PlateNumber.Option.IfNumberNotExist.Retry;
            lblTaskIfNumberNotExistRetryTimes.Visible = (PlateNumber.Option.IfNumberNotExist)ddlTaskIfNumberNotExist.SelectedIndex == PlateNumber.Option.IfNumberNotExist.Retry;
        }

        private void ddlTaskIfNumberHasBeUsed_SelectedIndexChanged(object sender, EventArgs e) {
            chkTaskIfNumberHasBeUsedRetryTimes.Visible = (PlateNumber.Option.IfNumberHasBeUsed)ddlTaskIfNumberHasBeUsed.SelectedIndex == PlateNumber.Option.IfNumberHasBeUsed.Retry;
            numTaskIfNumberHasBeUsedRetryTimes.Visible = (PlateNumber.Option.IfNumberHasBeUsed)ddlTaskIfNumberHasBeUsed.SelectedIndex == PlateNumber.Option.IfNumberHasBeUsed.Retry;
            lblTaskIfNumberHasBeUsedRetryTimes.Visible = (PlateNumber.Option.IfNumberHasBeUsed)ddlTaskIfNumberHasBeUsed.SelectedIndex == PlateNumber.Option.IfNumberHasBeUsed.Retry;
        }

        private void chkTaskIfFoundUnknownPageRetryTimes_CheckedChanged(object sender, EventArgs e) {
            numTaskIfFoundUnknownPageRetryTimes.Enabled = chkTaskIfFoundUnknownPageRetryTimes.Checked;
            lblTaskIfFoundUnknownPageRetryTimes.Enabled = chkTaskIfFoundUnknownPageRetryTimes.Checked;
        }

        private void chkTaskIfNumberNotExistRetryTimes_CheckedChanged(object sender, EventArgs e) {
            numTaskIfNumberNotExistRetryTimes.Enabled = chkTaskIfNumberNotExistRetryTimes.Checked;
            lblTaskIfNumberNotExistRetryTimes.Enabled = chkTaskIfNumberNotExistRetryTimes.Checked;
        }

        private void chkTaskIfNumberHasBeUsedRetryTimes_CheckedChanged(object sender, EventArgs e) {
            numTaskIfNumberHasBeUsedRetryTimes.Enabled = chkTaskIfNumberHasBeUsedRetryTimes.Checked;
            lblTaskIfNumberHasBeUsedRetryTimes.Enabled = chkTaskIfNumberHasBeUsedRetryTimes.Checked;
        }
    }
}
